package com.exp.sys.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.exp.base.util.PathUtils;
import com.exp.sys.bean.shiro.AuthenticationToken;

@Controller
@RequestMapping("/sys/main")
public class MainAction extends BaseAction {
	
	@RequestMapping("index")
	public String index(){
		return "sys/main/main_index";
	}
	
	@RequestMapping("login")
	public String login(HttpServletRequest request,Model model) {
		System.out.println("login");
		Subject subject = SecurityUtils.getSubject();
		if(subject.isAuthenticated()){
			return "redirect:/sys/main/index";
		}else{
			Map<String, ?> attributes = RequestContextUtils.getInputFlashMap(request);
			model.addAllAttributes(attributes);
			return "sys/main/main_login";
		}
	}

	@RequestMapping("loginSubmit")
	public String loginSubmit(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException {
		System.out.println("loginSubmit");
		Subject subject = SecurityUtils.getSubject();
		if(subject.isAuthenticated()){
			return "redirect:/sys/main/index";
		}else{
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			boolean rememberMe = StringUtils.equals(request.getParameter("rememberMe"),"true");
			String host = request.getRemoteHost();
			AuthenticationToken token =  new AuthenticationToken(username, password,"", "", rememberMe, host);
			try {
				subject.login(token);
				WebUtils.redirectToSavedRequest(request, response, "/sys/main/index");
			} catch (AuthenticationException e) {
				e.printStackTrace();
				WebUtils.getAndClearSavedRequest(request);
				String errorMsg = "登录失败";
				if(e instanceof IncorrectCredentialsException){
					errorMsg = "密码错误";
				}else if(e instanceof AccountException){
					errorMsg = "账号错误";
				}
				redirectAttributes.addFlashAttribute("errorMsg",errorMsg);
				redirectAttributes.addFlashAttribute("username", username);
				redirectAttributes.addFlashAttribute("rememberMe",rememberMe);
				return "redirect:/sys/main/login";
			}
			return null;
		}
	}

/*	@RequestMapping("logout" )
	public String logout() {
		return "main_logout";
	}
*/
}
